﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Jitter.Dynamics;
using Jitter.LinearMath;

namespace X13.Libs
{
    public abstract class PhysicsPrim : RigidBody
    {
        //Fields
        protected Vector3 Scale;
        private Vector3 rotation;

        public Vector3 Rotation
        {
            get { return rotation; }
            private set
            {
                rotation = value;
                this.Orientation = Converter.ConvertMatrix(Matrix.CreateRotationX(Rotation.X) *
                    Matrix.CreateRotationY(Rotation.Y) * Matrix.CreateRotationZ(Rotation.Z));
            }
        }

        //Construct
        public PhysicsPrim(Vector3 Position)
            : base(new Jitter.Collision.Shapes.BoxShape(Converter.ConvertVect(Vector3.One)))
        {
            this.AffectedByGravity = false;
            this.IsStatic = true;
            this.Material = new Jitter.Dynamics.Material();
            this.Material.Restitution = 0f; 
            this.Rescale(Vector3.One);
            this.Position = Converter.ConvertVect(Position);
        }

        //Methods
        public void Rescale(Vector3 scale)
        {
            this.Scale = scale;
            this.Shape = new Jitter.Collision.Shapes.BoxShape(Converter.ConvertVect(this.Scale));
            this.Shape.UpdateShape();
            AdjustHeight();
        }
        public void AdjustHeight()
        {
            this.Position = new JVector(this.Position.X, (this.Scale.Y / 2f), this.Position.Z);
        }
                
        public void SetRotation(float Xdegs, float Ydegs, float Zdegs)
        {
            this.Rotation = new Vector3(
                MathHelper.ToRadians(Xdegs),
                MathHelper.ToRadians(Ydegs),
                MathHelper.ToRadians(Zdegs));
        }
        public void Rotate(float Xdegs, float Ydegs, float Zdegs)
        {
            this.Rotation += new Vector3(
                MathHelper.ToRadians(Xdegs),
                MathHelper.ToRadians(Ydegs),
                MathHelper.ToRadians(Zdegs));
        }
    }
}